home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Harvest C 1.3 / Source Code / DynArray.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-15  |  2.8 KB  |  111 lines  |  [TEXT/ALFA]

  1. /*
  2.     Harvest C
  3.     Copyright 1992 Eric W. Sink.  All rights reserved.
  4.     
  5.     This file is part of Harvest C.
  6.     
  7.     Harvest C is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU Generic Public License as published by
  9.     the Free Software Foundation; either version 2, or (at your option)
  10.     any later version.
  11.     
  12.     Harvest C is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.     
  17.     You should have received a copy of the GNU General Public License
  18.     along with Harvest C; see the file COPYING.  If not, write to
  19.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.     
  21.     Harvest C is not in any way a product of the Free Software Foundation.
  22.     Harvest C is not GNU software.
  23.     Harvest C is not public domain.
  24.  
  25.     This file may have other copyrights which are applicable as well.
  26.  
  27. */
  28.  
  29. /*
  30.  * Harvest C
  31.  * 
  32.  * Copyright 1991 Eric W. Sink   All rights reserved.
  33.  * 
  34.  * This file implements operations on dynamic arrays.
  35.  * 
  36.  */
  37.  
  38. #include "conditcomp.h"
  39. #include <stdio.h>
  40. #include <string.h>
  41.  
  42. #include "structs.h"
  43.  
  44. #pragma segment DynArrays
  45.  
  46. #include "DynArray.h"
  47.  
  48.  
  49. struct DynArray {
  50.     EString_t                        Objects;
  51.     unsigned long                   ObjectSize;
  52.     unsigned long                   ArrayObjCapacity;
  53.     unsigned long                   CountObjects;
  54. };
  55.  
  56. DynArrayVia_t
  57. RawDynArray(unsigned long objsize, unsigned long startcapacity)
  58. {
  59.     DynArrayVia_t                   raw;
  60.     raw = Ealloc(sizeof(DynArray_t));
  61.     if (raw) {
  62.     Via(raw)->Objects = Ealloc(objsize * startcapacity);
  63.     Via(raw)->ObjectSize = objsize;
  64.     Via(raw)->ArrayObjCapacity = startcapacity;
  65.     Via(raw)->CountObjects = 1;    /* We start with 1 so 0 is invalid */
  66.     }
  67.     return raw;
  68. }
  69.  
  70. void
  71. GrowDynArray(DynArrayVia_t da)
  72. {
  73. #ifdef OLDMEM
  74.     SetHandleSize((Handle) Via(da)->Objects, Via(da)->ObjectSize * Via(da)->ArrayObjCapacity * 1.5);
  75. #else
  76.     char *newMem;
  77.     newMem = (char *) icemalloc((long) (Via(da)->ObjectSize * Via(da)->ArrayObjCapacity * 1.5));
  78.     memcpy(newMem,Via(da)->Objects,Via(da)->ObjectSize * Via(da)->ArrayObjCapacity);
  79.     icefree(Via(da)->Objects);
  80.     Via(da)->Objects = newMem;
  81. #endif
  82.     Via(da)->ArrayObjCapacity *= 1.5;
  83. }
  84.  
  85. ObjID
  86. AddObject(DynArrayVia_t da)
  87. {
  88.     unsigned long                   result;
  89.     if ((Via(da)->CountObjects + 5) >= Via(da)->ArrayObjCapacity) {
  90.         GrowDynArray(da);
  91.     }
  92.     memset(&(Via(Via(da)->Objects)[result = (Via(da)->CountObjects) * Via(da)->ObjectSize]), 0L, Via(da)->ObjectSize);
  93.     Via(da)->CountObjects++;
  94.     return result;
  95. }
  96.  
  97. ObjRef
  98. GetObject(DynArrayVia_t da, ObjID obj)
  99. {
  100.     return &(Via(Via(da)->Objects)[obj]);
  101. }
  102.  
  103. void
  104. KillDynArray(DynArrayVia_t da)
  105. {
  106.     if (da) {
  107.     Efree(Via(da)->Objects);
  108.     Efree(da);
  109.     }
  110. }
  111.